package com.wealth.system.web.controller;

import iaas.utils.RedirectMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.system.entity.Corporation;
import com.wealth.system.entity.User;
import com.wealth.system.service.ICorpService;
import com.wealth.system.service.IUserService;
import com.wealth.system.service.ShiroUser;

/** 
* 系统设置
* @author chan
*/
@Controller
@RequestMapping(value="/manage/system/setting")
public class SettingController extends BaseController{
	
	@Autowired
	private IUserService userService;
	@Autowired
	private ICorpService corpService;
	
	Logger logger = Logger.getLogger(SettingController.class);
	
	@RequestMapping(value="/password")
	public String passwordForm(Model model){
		try{
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			//企业
			Corporation corp = corpService.get(corpid); 
			//企业管理员
			User user = userService.get(ShiroUser.getInstance().getUser().getId());
			model.addAttribute("corp", corp);
			model.addAttribute("user", user);
			return "system/password";
		}catch(Exception e){
			logger.error("密码设置页面错误", e);
			return "system/500";
		}
	}
	
	@RequestMapping(value="/password",method = RequestMethod.POST)
	public String updatePassword(Model model,User user,RedirectAttributes redirectAttributes){
		try{
			userService.updatePassword(user);
			redirectAttributes.addFlashAttribute("message",  RedirectMessage.setShowTipSuccess("密码设置操作成功"));
			return "redirect:/manage/system/setting/password";
		}catch(Exception e){
			logger.error("密码设置错误", e);
			return "system/500";
		}
	}
}
